package com.freak.neteasecloudmusic.modules.find.recommend.songlist.all;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.base.BaseAbstractSimpleActivity;
import com.freak.neteasecloudmusic.base.BaseAbstractSimpleWithPlayerActivity;
import com.freak.neteasecloudmusic.base.IActivityStatusBar;

import com.freak.neteasecloudmusic.dagger.component.DaggerDaggerComponent;
import com.freak.neteasecloudmusic.modules.find.recommend.entity.SongListCategoryEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.SongListContract;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.SongListPresenter;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.all.detail.SongClassifyListActivity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.all.entity.MyMultiItemEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.all.entity.SongTypeEntity;
import com.freak.neteasecloudmusic.net.log.LogUtil;
import com.freak.neteasecloudmusic.view.custom.toolbar.SimpleToolbar;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 所有歌单分类
 *
 * @author Administrator
 */
public class AllClassifyActivity extends BaseAbstractSimpleWithPlayerActivity implements IActivityStatusBar, SongListContract.View {
    @BindView(R.id.textViewAllClassifyTip)
    TextView textViewAllClassifyTip;
    @BindView(R.id.textViewAllClassifyEdit)
    TextView textViewAllClassifyEdit;
    @BindView(R.id.recyclerViewAllClassify)
    RecyclerView recyclerViewAllClassify;
    @BindView(R.id.bottom_container)
    FrameLayout bottomContainer;
    @BindView(R.id.simpleToolbarAllClassify)
    SimpleToolbar simpleToolbarAllClassify;
    private AllClassifyAdapter allClassifyAdapter;
    private List<MyMultiItemEntity> list;
    private List<SongListCategoryEntity.SubBean> selectList;
    private Bundle bundle;
    @Inject
    SongListPresenter songListPresenter;

    @Override
    protected int getLayout() {
        return R.layout.activity_all_song_list;
    }

    @Override
    protected void initEventAndData() {

    }

    @Override
    protected void onCreateLoadData() {
        
    }

    @Override
    protected void onDestroyRelease() {
        songListPresenter.detachView();
    }

    @Override
    protected void onResumeLoadData() {

    }


    @Override
    protected void initView() {
        DaggerDaggerComponent.builder().view(this).build().inject(this);
        simpleToolbarAllClassify.setLeftIconClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        selectList = new ArrayList<>();
        bundle = getIntent().getExtras();
        if (bundle == null) {
            finish();
            return;
        }
        selectList = (List<SongListCategoryEntity.SubBean>) bundle.getSerializable("list");
        list = new ArrayList<>();
        recyclerViewAllClassify.setLayoutManager(new GridLayoutManager(mActivity, 4));
        allClassifyAdapter = new AllClassifyAdapter(list);

        recyclerViewAllClassify.setAdapter(allClassifyAdapter);
        allClassifyAdapter.setEmptyView(R.layout.view_no_data_common, recyclerViewAllClassify);
        allClassifyAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                //SpanSize代表的是占有多少个spanCount
                return list == null || list.size() <= 0 ? 1 : list.get(position).getSpanSize();
            }
        });

        allClassifyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SongListCategoryEntity.SubBean subBean = (SongListCategoryEntity.SubBean) adapter.getData().get(position);
                Bundle bundle = new Bundle();
                bundle.putString("cat", subBean.getName());
                gotoActivity(SongClassifyListActivity.class, false, bundle);
            }
        });
        LogUtil.e("开始请求");
        songListPresenter.loadSongListCategory();
    }

    @Override
    public int getStatusBarColor() {
        return ContextCompat.getColor(this, R.color.color_fbfbfb);
    }

    @Override
    public int getDrawableStatusBar() {
        return 0;
    }


    @Override
    public void showToast(String toast) {

    }


    @OnClick({R.id.textViewAllClassifyTip, R.id.textViewAllClassifyEdit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.textViewAllClassifyTip:
                break;
            case R.id.textViewAllClassifyEdit:
                break;
        }
    }

    @Override
    public void loadSongListCategorySuccess(SongListCategoryEntity songListCategoryEntity) {
        if (selectList != null && selectList.size() > 0) {
            list.addAll(selectList);
        }
        if (songListCategoryEntity != null) {
            if (songListCategoryEntity.getCategories() != null) {
                SongTypeEntity songTypeEntity = new SongTypeEntity();
                songTypeEntity.setSongType(songListCategoryEntity.getCategories().get_$0());
                list.add(songTypeEntity);
                addSongTypeName(songListCategoryEntity, "0");

                SongTypeEntity songTypeEntity1 = new SongTypeEntity();
                songTypeEntity1.setSongType(songListCategoryEntity.getCategories().get_$1());
                list.add(songTypeEntity1);
                addSongTypeName(songListCategoryEntity, "1");

                SongTypeEntity songTypeEntity2 = new SongTypeEntity();
                songTypeEntity2.setSongType(songListCategoryEntity.getCategories().get_$2());
                list.add(songTypeEntity2);
                addSongTypeName(songListCategoryEntity, "2");

                SongTypeEntity songTypeEntity3 = new SongTypeEntity();
                songTypeEntity3.setSongType(songListCategoryEntity.getCategories().get_$3());
                list.add(songTypeEntity3);
                addSongTypeName(songListCategoryEntity, "3");

                SongTypeEntity songTypeEntity4 = new SongTypeEntity();
                songTypeEntity4.setSongType(songListCategoryEntity.getCategories().get_$0());
                list.add(songTypeEntity4);
                addSongTypeName(songListCategoryEntity, "4");
            }
        }
        allClassifyAdapter.notifyDataSetChanged();
    }

    private void addSongTypeName(SongListCategoryEntity songListCategoryEntity, String type) {
        for (SongListCategoryEntity.SubBean subBean : songListCategoryEntity.getSub()) {
            if (TextUtils.equals(subBean.getCategory(), type)) {
                list.add(subBean);
            }
        }
    }
}
